package edu.csula.cs575;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

public class EmailViewAdaptor extends ArrayAdapter<EmailView> /*implements AdapterView.OnItemClickListener*/ {

	private ArrayList<EmailView> items;
	private Context context;

	public EmailViewAdaptor(Context context, int textViewResourceId, ArrayList<EmailView> items) {
		super(context, textViewResourceId, items);
		this.items = items;
		this.context = context ;
	}

	static class ViewHolder {
		protected TextView email;
		
		protected CheckBox checkBoxCheck ;
	}

	public EmailView GetItemAtPosition(int position)
	{
		return items.get(position);
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		if (convertView == null) {
			LayoutInflater layoutInflator = ((Activity)context).getLayoutInflater();
			view = layoutInflator.inflate(R.layout.emailentry, null);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.email = (TextView) view.findViewById(R.id.email_entry);
			viewHolder.checkBoxCheck = (CheckBox) view.findViewById(R.id.checkBoxCheck2);
			viewHolder.checkBoxCheck
			.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					EmailView element = (EmailView) viewHolder.checkBoxCheck.getTag();
					if(buttonView.isChecked() == false )
					{
						element.setChecked(false );
						Log.d("Checked : " + element.getEmail(), "This is not checked");
					}
					else{
						element.setChecked(true);
						Log.d("Checked : " + element.getEmail(), "This is checked");
					}

				}
			});
			view.setTag(viewHolder);
			viewHolder.checkBoxCheck.setTag(items.get(position));
		} else {
			view = convertView;
			((ViewHolder) view.getTag()).checkBoxCheck.setTag(items.get(position));
		}
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.email.setText(items.get(position).getEmail());
		holder.checkBoxCheck.setChecked(items.get(position).isChecked());
		return view;
	}


	/*@Override
	public void onItemClick(AdapterView<?> parent, View convertView, int position, long id) {
		Toast.makeText(context, "Postion:" + position, Toast.LENGTH_SHORT).show();		
	}*/



}
